
//  Definition for singly-linked list.
function ListNode(val, node) {
    this.val = val;
    this.next = node || null;
}

/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
var addTwoNumbers = function (l1, l2) {
    let flag = false;
    let rs = new ListNode(0);
    let tmp = rs;
    while(l1 !== null || l2 !== null || flag) {
        let a = (l1 ? l1.val : 0) + (l2 ? l2.val : 0) + (flag ? 1 : 0);
        // console.log(a)
        flag = a >= 10;
        tmp.next = new ListNode(a % 10)
        tmp = tmp.next
        l1 = l1 !== null ? l1.next : l1;
        l2 = l2 !== null ? l2.next : l2;
    }
    return rs.next
};

function print(l3) {
    while(l3 !== null) {
        console.log(l3.val)
        l3 = l3.next
    }
}

let l1 = new ListNode(2, new ListNode(4, new ListNode(3)))
let l2 = new ListNode(5, new ListNode(6, new ListNode(4)))
// console.log(l1.next.val)
print(addTwoNumbers(l1, l2))
console.log('---')
let l3 = addTwoNumbers(new ListNode(5), new ListNode(5))
// let a = null;
// let b = a ?? 1;
// console.log(b)
print(l3)

console.log('end')
